package com.example.config;

import com.example.constant.RabbitmqConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitmqConfig {


    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(RabbitmqConstant.TICKET_EXCHANGE,true,false);
    }

    @Bean
    public TopicExchange DeadLetterExchange(){
        return new TopicExchange(RabbitmqConstant.TICKET_DEAD_LETTER_EXCHANGE,true,false);
    }

    @Bean
    public Queue selectQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange",RabbitmqConstant.TICKET_DEAD_LETTER_EXCHANGE);
        map.put("x-dead_letter-routing-key",RabbitmqConstant.TICKET_DEAD_LETTER_KEY);
        map.put("x-message-ttl",30000);
        return new Queue(RabbitmqConstant.TICKET_SELECT_QUEUE,true,false,false,map);
    }

    @Bean
    public Queue deadLetterQueue(){
        return new Queue(RabbitmqConstant.TICKET_DEAD_LETTER_QUEUE,true);
    }

    @Bean
    public Queue insertQueue(){
        return new Queue(RabbitmqConstant.TICKET_INSERT_QUEUE,true);
    }

    @Bean
    public Queue updateQueue(){
        return new Queue(RabbitmqConstant.TICKET_UPDATE_QUEUE,true);
    }

    //绑定查询队列
    @Bean
    public Binding selectQueueBinding(){
        return  BindingBuilder.bind(selectQueue()).to(topicExchange()).with(RabbitmqConstant.TICKET_SELECT_KEY);
    }

    //绑定死信队里
    @Bean
    public Binding deadLetterBinding(){
        return BindingBuilder.bind(deadLetterQueue()).to(DeadLetterExchange()).with(RabbitmqConstant.TICKET_DEAD_LETTER_KEY);
    }

    @Bean
    public Binding insertQueueBinding(){
        return  BindingBuilder.bind(insertQueue()).to(topicExchange()).with(RabbitmqConstant.TICKET_INSERT_KEY);
    }

    @Bean
    public Binding updateQueueBinding(){
        return  BindingBuilder.bind(updateQueue()).to(topicExchange()).with(RabbitmqConstant.TICKET_UPDATE_KEY);
    }


}
